package kr.hyunist.androidcomplete01saeildamil.ch12;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.*;

public class GalleryEx extends Activity {
	int[] picture = { R.drawable.ccdam, R.drawable.soyang2, R.drawable.ududong,
			R.drawable.zipdarigol, R.drawable.dongul };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.galleryex);

		GalleryAdapter adapter = new GalleryAdapter(this, picture);
		Gallery gal = (Gallery) findViewById(R.id.gallerytest);
		gal.setAdapter(adapter);
		gal.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(GalleryEx.this, position + "번재", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
			
		});
		
	}
}

class GalleryAdapter extends BaseAdapter {
	Context mContext;
	int[] picture;

	public GalleryAdapter(Context context, int[] pic) {
		mContext = context;
		picture = pic;
	}

	@Override
	public int getCount() {
		return picture.length;
	}

	@Override
	public Object getItem(int position) {
		return picture[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageview;
		
		if(convertView == null){
			imageview = new ImageView(mContext);
		} else {
			imageview = (ImageView) convertView;
		}
		imageview.setLayoutParams(new Gallery.LayoutParams(136, 88));
		imageview.setScaleType(ImageView.ScaleType.FIT_XY);
		imageview.setImageResource(picture[position]);
		return imageview;
	}

}